package com.yuyu.qingmangmall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.yuyu.common.constant.WareConstant;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.Query;
import com.yuyu.qingmangmall.ware.dao.PurchaseDetailDao;
import com.yuyu.qingmangmall.ware.entity.PurchaseDetailEntity;
import com.yuyu.qingmangmall.ware.service.PurchaseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Autowired
    PurchaseDetailDao purchaseDetailDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询采购需求
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<PurchaseDetailEntity>();
        /*
        key: '华为',//检索关键字  id  sku_id
        status: 0,//状态
        wareId: 1,//仓库id
        */
        String key = (String) params.get("key");
        if (!StringUtils.isNullOrEmpty(key)) {
            wrapper.and(wrap->{
                wrap.eq("id",key).or().eq("sku_id",key);
            });

        }

        String status = (String) params.get("status");
        if (!StringUtils.isNullOrEmpty(status)) {
            wrapper.eq("status",status);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isNullOrEmpty(wareId)) {
            wrapper.eq("ware_id",wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params),
                wrapper);

        return new PageUtils(page);
    }

    /**
     * 根据PurchaseIds 拿到 关联的采购需求
     * @param ids 采购单is们
     * @return
     */
    @Override
    public List<PurchaseDetailEntity> selectPurchaseDetailByPurchaseIds(List<Long> ids) {

        List<PurchaseDetailEntity> detailEntities = new ArrayList<PurchaseDetailEntity>();
        // 遍历所有采购单is
        ids.forEach(id->{
            QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();

            wrapper.eq("purchase_id",id);
            // 拿到相应的采购需求
            detailEntities.addAll(this.baseMapper.selectList(wrapper));
        });


        return detailEntities;
    }

    /**
     *  将采购需求改为已分配改为
     * @param collect
     */
    @Override
    public void updateBatchByIdAndStatusIsAssigned(List<PurchaseDetailEntity> collect) {
        List<PurchaseDetailEntity> collect1 = collect.stream().map(purchaseDetail -> {
            purchaseDetail.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetail;
        }).collect(Collectors.toList());

        this.updateBatchById(collect1);
    }

}